/*
 * @Descripttion:
 * @version:
 * @Author: sueRimn
 * @Date: 2025-07-29 21:55:12
 * @LastEditors: sueRimn
 * @LastEditTime: 2025-08-26 19:51:08
 */

#include "key.h"
#include "string.h"
#include "stdio.h"
#include "stdlib.h"

#include "public.h"
#include "power.h"
#include "led.h"

dev_key_t pstHeadKeyNode;

dev_key_t key_regist(GPIO_TypeDef *pstGpioPort, uint16_t usGpioPin, uint16_t uiLongTime,
    void (*key_down)(void *param), void (*key_short)(void *param), void (*key_long)(void *param), void *param)
{
    dev_key_t pstKeyNode = (dev_key_t)malloc(sizeof(dev_key));
    // printf("pstKeyNode addr = %x\r\n", pstKeyNode);
    pstKeyNode->pstGpioPort = pstGpioPort;
    pstKeyNode->usGpioPin = usGpioPin;
    pstKeyNode->uiLongTime = uiLongTime;
    pstKeyNode->key_down = key_down;
    pstKeyNode->key_short = key_short;
    pstKeyNode->key_long = key_long;
    pstKeyNode->param = param;

    pstKeyNode->pstNextKey = NULL;

    pstKeyNode->ucLastStat = (uint8_t)GPIO_PIN_SET;

    return pstKeyNode;
}

void key_run()
{
    uint8_t ucKeyStat;

    dev_key_t pstKeyNode = pstHeadKeyNode;

    while (pstKeyNode->pstNextKey != NULL)
    {
        pstKeyNode = pstKeyNode->pstNextKey;
        ucKeyStat = (uint8_t)HAL_GPIO_ReadPin(pstKeyNode->pstGpioPort, pstKeyNode->usGpioPin);

        if ((ucKeyStat == 0) && (pstKeyNode->ucLastStat == 1)) //按下按键
        {
            pstKeyNode->uiDownTime = pstDev->uiSysMsTime;
            if (pstKeyNode->key_down != NULL)
                pstKeyNode->key_down(pstKeyNode->param);
        }
        else if ((ucKeyStat == 1) && (pstKeyNode->ucLastStat == 0)) //按键松开
        {
            if (((pstDev->uiSysMsTime - pstKeyNode->uiDownTime) > KEY_SHORT_MIN_TIME)
                && ((pstDev->uiSysMsTime - pstKeyNode->uiDownTime) <= pstKeyNode->uiLongTime))//达到短按标准
            {
                if (pstKeyNode->key_short != NULL)
                    pstKeyNode->key_short(pstKeyNode->param);
            }
            else if ((pstDev->uiSysMsTime - pstKeyNode->uiDownTime) > pstKeyNode->uiLongTime)//达到长按标准
            {
                if (pstKeyNode->key_long != NULL)
                    pstKeyNode->key_long(pstKeyNode->param);
            }
        }
        pstKeyNode->ucLastStat = ucKeyStat;
    }
}

void key_1_short_callback(void *param)
{
    printf("key1 short\r\n");
}
void key_1_long_callback(void *param)
{
    printf("key1 long\r\n");
}

void key_2_short_callback(void *param)
{
    printf("key2 short\r\n");
}
void key_2_long_callback(void *param)
{
    printf("key2 long\r\n");
}

void key_3_short_callback(void *param)
{
    printf("key3 short\r\n");
}
void key_3_long_callback(void *param)
{
    printf("key3 long\r\n");
}

void key_soft_switch_down_callback(void *param)
{
    canReportSwitch(1, 1, 0);
    // printf("short down = %d \r\n", pstDev->uiSysMsTime - *(uint32_t *)param);
}

void key_soft_switch_short_callback(void *param)
{
    canReportSwitch(1, 0, 0);
    // printf("short down = %d \r\n", pstDev->uiSysMsTime - *(uint32_t *)param);
}

void key_soft_switch_long_callback(void *param)
{
    if (pstDev->ucPowerStat == 0)//未上电状态
    {
        powerOn();
    }
    else if (pstDev->ucPowerStat == 1)
    {
        powerOff();
    }

}


void key_init()
{
    pstHeadKeyNode = (dev_key_t)malloc(sizeof(dev_key));

    dev_key_t pstKeyNode;
    pstKeyNode = pstHeadKeyNode;

    pstKeyNode->pstNextKey = key_regist(KEY_3_GPIO_Port, KEY_3_Pin, 2000,
        NULL, key_1_short_callback, key_1_long_callback, NULL);
    pstKeyNode = pstKeyNode->pstNextKey;

    pstKeyNode->pstNextKey = key_regist(KEY_2_GPIO_Port, KEY_2_Pin, 2000,
        NULL, key_2_short_callback, key_2_long_callback, NULL);
    pstKeyNode = pstKeyNode->pstNextKey;
    pstKeyNode->param = (void *)&(pstKeyNode->uiDownTime);

    pstKeyNode->pstNextKey = key_regist(KEY_1_GPIO_Port, KEY_1_Pin, 2000,
        NULL, key_3_short_callback, key_3_long_callback, NULL);
    pstKeyNode = pstKeyNode->pstNextKey;

    //    pstKeyNode->pstNextKey = key_regist(POWER_B_GPIO_Port, POWER_B_Pin, 3000,
    //        key_soft_switch_short_callback, key_soft_switch_long_callback, NULL);
    //    pstKeyNode = pstKeyNode->pstNextKey;
    //    pstKeyNode->param = (void *)&(pstKeyNode->uiDownTime);

}